home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1146 / 1146.xpi / chrome / screengrab.jar / content / ScrnShots.js < prev    next >
Text File  |  2009-03-09  |  4KB  |  86 lines

  1.  
  2. screengrab.ScrnShots = function(user, pass) {
  3.     this.user = user;
  4.     this.pass = pass;
  5. };
  6. screengrab.ScrnShots.prototype = {
  7.     getTags: function() {
  8.         var req = new XMLHttpRequest();
  9.         req.open("GET", "http://www.scrnshots.com/users/" + this.user + "/tags.XML", true);
  10.         req.overrideMimeType('text/xml');
  11.         req.onreadystatechange = function() {
  12.             if (req.readyState == 4 && req.status == 200) {
  13.                 var names = req.responseXML.getElementsByTagName('name');
  14.                 var tags = names.map(new function(name) {
  15.                     return name.childNodes[0].nodeValue;
  16.                 });
  17.                 return tags;
  18.             }
  19.             throw "Couldn't get tags";
  20.         }
  21.         req.send(null);
  22.     },
  23.     
  24.     upload: function(shotData, onUploaded) {
  25.         var boundStr = '---------------------------265001916915724';
  26.         var boundary = '--' + boundStr;
  27.         var req = new XMLHttpRequest();
  28.         var bodyStart = 
  29.             boundary + "\r\n" +
  30.             'Content-Disposition: form-data; name="screenshot[description]"' + "\r\n" +
  31.             "\r\n" +
  32.             shotData.description + "\r\n" +
  33.             boundary + "\r\n" +
  34.             'Content-Disposition: form-data; name="screenshot[tag_list]"' + "\r\n" +
  35.             "\r\n" +
  36.             shotData.tags + "\r\n" +
  37.             boundary + "\r\n" +
  38.             'Content-Disposition: form-data; name="screenshot[source_url]"' + "\r\n" +
  39.             "\r\n" +
  40.             shotData.sourceUri + "\r\n" +
  41.             boundary + "\r\n" +
  42.             'Content-Disposition: form-data; name="screenshot[uploaded_data]"; filename="' + shotData.uploadFilename + '"' + "\r\n" +
  43.             'Content-Type: image/png' + "\r\n" +
  44.             "\r\n";
  45.         var bodyStartStream = new sg.File.StringInputStream(bodyStart, bodyStart.length);
  46.         var fileStream = shotData.file.getBufferedStream();
  47.         var bodyEnd = 
  48.             "\r\n" +
  49.             boundary + "--";
  50.         var bodyEndStream = new sg.File.StringInputStream(bodyEnd, bodyEnd.length);
  51.         
  52.         var multiplexed = new sg.File.MultiplexInputStream();
  53.         multiplexed.appendStream(bodyStartStream);
  54.         multiplexed.appendStream(fileStream);
  55.         multiplexed.appendStream(bodyEndStream);
  56.         var length = bodyStartStream.available() + fileStream.available() + bodyEndStream.available(); 
  57.         req.open("POST", "http://www.scrnshots.com/screenshots.xml", true);
  58.         req.setRequestHeader("Authorization", "Basic " + this.getAuthHeader());
  59.         req.setRequestHeader("Content-type", "multipart/form-data; boundary=" + boundStr);
  60.         req.setRequestHeader("Content-Length", length);
  61.         req.setRequestHeader("Connection", "close");
  62.         req.overrideMimeType('text/xml');
  63.         req.onreadystatechange = function() {
  64.             if (req.readyState == 4) {
  65.                 var links = req.responseXML.getElementsByTagName('link');
  66.                 if (links.length != 1) {
  67.                     alert("Something went wrong...");
  68.                 } else {
  69.                     gBrowser.selectedTab = gBrowser.addTab(links[0].childNodes[0].nodeValue);
  70.                 }
  71.                 shotData.file.remove();
  72.             }
  73.         }
  74.         req.send(multiplexed);
  75.     },
  76.     getAuthHeader: function(){
  77.         return "Basic " + sg.Base64.encode(this.user + ":" + this.pass);
  78.     }
  79. }
  80. screengrab.ScrnShots.ShotData = function() {
  81.     this.file = null;
  82.     this.tags = "";
  83.     this.description = "";
  84.     this.sourceUri = "";
  85.     this.uploadFilename = "";
  86. }